package com.nervenets.shopping.hibernate.domain.shopping;

import com.alibaba.fastjson.annotation.JSONField;
import com.fasterxml.jackson.annotation.JsonIgnore;
import com.nervenets.general.adapter.jsonType.StringArrayJson;
import com.nervenets.general.hibernate.DomainObject;
import com.nervenets.general.jwt.util.JwtUtils;
import com.nervenets.general.model.SecurityUser;
import com.nervenets.general.utils.SpringContextHolder;
import com.nervenets.shopping.hibernate.domain.common.GlobalRuleSetting;
import com.nervenets.shopping.hibernate.domain.order.GroupBuyOrder;
import com.nervenets.shopping.service.GlobalRuleService;
import io.swagger.annotations.ApiModelProperty;
import lombok.*;
import lombok.experimental.SuperBuilder;
import org.hibernate.annotations.Type;

import javax.persistence.*;
import java.util.List;
import java.util.stream.Collectors;

@EqualsAndHashCode(callSuper = true)
@Entity
@Table
@Setter
@Getter
@AllArgsConstructor
@NoArgsConstructor
@SuperBuilder
public class Product extends DomainObject {
    @ApiModelProperty(value = "商品名称")
    private String name;
    @ApiModelProperty(value = "封面图")
    private String coverImage;
    @ApiModelProperty(value = "分享封面图")
    private String shareCoverImage;
    @ApiModelProperty(value = "轮播图")
    @Type(type = StringArrayJson.NAME)
    @Column(columnDefinition = "json")
    private List<String> pictures;
    @ApiModelProperty(value = "价格")
    private int price;
    @ApiModelProperty(value = "原价格")
    private int originalPrice;
    @ApiModelProperty(value = "结算价格")
    private int settlePrice;
    @ApiModelProperty(value = "销售数量")
    @JsonIgnore
    @JSONField(serialize = false)
    private long saleCount;
    @ApiModelProperty(value = "基础销售数量")
    private long baseSaleCount;
    @ApiModelProperty(value = "拼团成功次数")
    private long groupSuccessCount;
    @ApiModelProperty(value = "排序")
    private int queue;
    @ApiModelProperty(value = "是否启用")
    private boolean enable = true;
    @ApiModelProperty(value = "详情描述")
    @Column(columnDefinition = "longtext")
    private String detail;
    @Builder.Default
    @ApiModelProperty(value = "成团人数")
    private int groupBuySuccess = 20;
    @Builder.Default
    @ApiModelProperty(value = "成功拼团人数")
    private int groupBuySuccessUser = 3;
    @Builder.Default
    @ApiModelProperty(value = "拼团失败奖励金额，0.3元-5000元区间有效，其他金额无效（单位：分）")
    private int groupBuyFailedAward = 500;
    @Builder.Default
    @ApiModelProperty(value = "拼团成团公益金额（单位：分）")
    private int groupBuyContribution = 300;
    @Builder.Default
    @ApiModelProperty(value = "拼团成团分红金额（单位：分）")
    private int groupBuyBonus = 2000;
    @ApiModelProperty(value = "开售时间")
    private long startSaleTime;
    @OneToOne
    @JoinColumn(name = "group_buy_id")
    private GroupBuyOrder currentGroupBuy;

    public long getTotalSaleCount() {
        return this.baseSaleCount + this.saleCount;
    }

    public int getNeedPeople() {
        GlobalRuleService globalRuleService = SpringContextHolder.getBean(GlobalRuleService.class);
        GlobalRuleSetting setting = globalRuleService.getGlobalRule().getSetting();
        if (null == currentGroupBuy) return groupBuySuccess;
        int size = currentGroupBuy.getUsers().size();
        return groupBuySuccess > size ? (groupBuySuccess - size) : groupBuySuccess;
    }

    public boolean isCurrentGroupBought() {
        if (null == this.currentGroupBuy) return false;
        SecurityUser user = JwtUtils.getUser();
        if (null == user) return false;
        List<Long> userIds = currentGroupBuy.getUsers().stream().map(DomainObject::getCreateBy).collect(Collectors.toList());
        return userIds.contains(user.getId());
    }
}
